import { Node } from "@/基本数据结构/n叉树";

export function levelOrder(root: Node | null): number[][] {
  if (!root) return [];
  const res: number[][] = [];
  const nodeArr: Node[] = [root];
  while (nodeArr.length) {
    const len = nodeArr.length;
    const tempArr: number[] = [];
    for (let index = 0; index < len; index++) {
      const node = nodeArr.shift();
      tempArr.push(node.val);
      node.children.forEach((child) => {
        nodeArr.push(child);
      });
    }
    res.push(tempArr);
  }

  return res;
}
